const functionRegExp = /^function/;
const functionRegExpName = /^\w+\s*\(/;
/**
 * 支持的格式
 * () => {}
 * value => {}
 * function() {}
 * setValue() {}
 * function setValue() {}
 */
export const transformStringToFunction = (str: string) => {
  try {
    if (typeof str !== 'string') return str;
    if (functionRegExpName.test(str) && !functionRegExp.test(str)) {
      str = `function ${str}`;
    }
    const fn = new Function(`
    return function() {
      try {
        const self = this
        return (${str}).apply(self, arguments)
      } catch(e) {
        console.log("字符串转函数失败:", e)
        return e.message
      }
    } 
  `)();
    return fn;
  } catch (e) {
    console.log('字符串转函数失败：', e);
  }
};
export const parseJSExpression = (value: any) => {
  try {
    return JSON.parse(value) as string;
  } catch (e) {
    return value as string;
  }
};
